<?php

namespace App\HttpController\Applet\Aikan;

use App\HttpController\BaseController;
use App\Utility\Cache;
use App\Utility\MiniProgram;
use EasySwoole\EasySwoole\Logger;

class AuthController extends BaseController
{
    protected $applet = 'aikan';
    protected $cacheTime = 30;
    public function index(){
        $request = $this->request();
        $code = $request->getRequestParam('code');
        $mini = new MiniProgram($this->applet);
        $session = $mini->session($code);
        if($session && isset($session['openid'])){
            //异步队列处理用户数据
            $queue = new Cache();
            $queue->onQueue('user_login');
            $queue->push(['openid'=>$session['openid'],'name'=>$this->applet]);
            //统计登录数据
            unset($queue);
            return $this->writeJson(0,$session);
        }
        //登录失败
        return $this->writeJson(1,null,'error');
    }
}
